package net.hn.hnms.biz.energy.company.service.impl;

import cn.hutool.core.util.StrUtil;
import net.hn.hnms.biz.energy.company.enums.MineInfo;
import net.hn.hnms.biz.energy.company.service.EnergyCompanyService;
import net.hn.hnms.biz.energy.mapper.*;
import net.hn.hnms.biz.energy.mapper.*;
import net.hn.hnms.biz.energy.service.PowerAnalysisService;
import net.hn.hnms.biz.energy.vo.*;
import net.hn.hnms.biz.energy.request.*;
import net.hn.hnms.biz.energy.request.*;
import net.hn.hnms.biz.energy.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class EnergyCompanyServiceImpl implements EnergyCompanyService {

    @Autowired
    private WaterPlacePriceMapper waterPlacePriceMapper;
    @Autowired
    private WaterSettlementMapper waterSettlementMapper;
    @Autowired
    private WaterSupplyCompanyServiceMapper waterSupplyCompanyServiceMapper;

    @Autowired
    private WaterRecordMapper waterRecordMapper;

    @Autowired
    private ElectricityPlacePriceMapper electricityPlacePriceMapper;

    @Autowired
    private ElectricitySettlementMapper electricitySettlementMapper;

    @Autowired
    private PowerAnalysisService powerAnalysisService;

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public List<WaterSupplyCompanyVo> getWaterSupplyCompanyByMineCode(WaterSupplyCompanyReq param) {

        Map<String, String> map = new HashMap<>();
        List<WaterSupplyCompanyVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(param.getCompanyCode()) && StringUtils.isAllBlank(param.getMineCode(), param.getMineName())) {
            map = MineInfo.getNameByCompanyCodeOfMap(param.getCompanyCode());
        } else {
            map = MineInfo.getNameByMineCodeAndName(param.getCompanyCode(), param.getMineCode(), param.getMineName());
        }

        for (String key : map.keySet()) {
            int count=waterSupplyCompanyServiceMapper.getWaterSupplyCompanyByMineCode(key);
            if(count<=0){
                continue;
            }
            WaterSupplyCompanyVo v = new WaterSupplyCompanyVo();
            v.setCompanyCode(param.getCompanyCode());
            v.setMineCode(key);
            v.setMineName(map.get(key));
            v.setWaterSupplyCompanyCount(count);
            list.add(v);
        }


        return list;
    }

    @Override
    public List<WaterPlacePriceVo> getWaterPlacePriceCompanyByMineCode(WaterPlacePriceReq param) {
        Map<String, String> map = new HashMap<>();
        List<WaterPlacePriceVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(param.getCompanyCode()) && StringUtils.isAllBlank(param.getMineCode(), param.getMineName())) {
            map = MineInfo.getNameByCompanyCodeOfMap(param.getCompanyCode());
        } else {
            map = MineInfo.getNameByMineCodeAndName(param.getCompanyCode(), param.getMineCode(), param.getMineName());
        }

        for (String key : map.keySet()) {
            int wscCount = waterSupplyCompanyServiceMapper.getWaterSupplyCompanyByMineCode(key);
            int wppCount = waterPlacePriceMapper.getWaterPlacePriceByMineCode(key);
            if(wscCount==0&&wppCount==0){
                continue;
            }
            WaterPlacePriceVo v = new WaterPlacePriceVo();
            v.setCompanyCode(param.getCompanyCode());
            v.setMineCode(key);
            v.setMineName(map.get(key));
            v.setWaterSupplyCompanyCount(wscCount);
            v.setWaterUnitCount(wppCount);
            list.add(v);
        }
        return list;
    }

    @Override
    public List<WaterSettlementVo> getWaterSettlementCompanyByMineCode(WaterSettlementReq param) {
        Map<String, String> map = new HashMap<>();
        List<WaterSettlementVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(param.getCompanyCode()) && StringUtils.isAllBlank(param.getMineCode(), param.getMineName())) {
            map = MineInfo.getNameByCompanyCodeOfMap(param.getCompanyCode());
        } else {
            map = MineInfo.getNameByMineCodeAndName(param.getCompanyCode(), param.getMineCode(), param.getMineName());
        }

        for (String key : map.keySet()) {
            int wscCount = waterSupplyCompanyServiceMapper.getWaterSupplyCompanyByMineCode(key);
            int wppCount = waterPlacePriceMapper.getWaterPlacePriceByMineCode(key);
            BigDecimal wsCount = waterSettlementMapper.getWaterSettlementByMineCode(key);
            if(wscCount==0&&wppCount==0&&(wsCount==null||wsCount.compareTo(new BigDecimal(0))==0)){
                continue;
            }
            WaterSettlementVo v = new WaterSettlementVo();
            v.setCompanyCode(param.getCompanyCode());
            v.setMineCode(key);
            v.setMineName(map.get(key));
            v.setWaterSupplyCompanyCount(wscCount);
            v.setWaterUnitCount(wppCount);
            v.setWaterConsumptionCount(wsCount);
            list.add(v);
        }
        return list;
    }

    @Override
    public List<WaterRecordVo> getWaterRecordByMineCode(WaterRecordReq param) {
        Map<String, String> map = new HashMap<>();
        List<WaterRecordVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(param.getCompanyCode()) && StringUtils.isAllBlank(param.getMineCode(), param.getMineName())) {
            map = MineInfo.getNameByCompanyCodeOfMap(param.getCompanyCode());
        } else {
            map = MineInfo.getNameByMineCodeAndName(param.getCompanyCode(), param.getMineCode(), param.getMineName());
        }

        for (String key : map.keySet()) {
            int wscCount = waterSupplyCompanyServiceMapper.getWaterSupplyCompanyByMineCode(key);
            BigDecimal wsCount=waterSettlementMapper.getWaterSettlementByMineCode(key);
            WaterRecordVo countVo = waterRecordMapper.getWaterRecordByMineCode(key);

            if(wscCount==0&&(wsCount==null||wsCount.compareTo(new BigDecimal(0))==0)&&countVo==null){
                continue;
            }

            WaterRecordVo v = new WaterRecordVo();
            v.setCompanyCode(param.getCompanyCode());
            v.setMineCode(key);
            v.setMineName(map.get(key));
            v.setWaterSupplyCompanyCount(waterSupplyCompanyServiceMapper.getWaterSupplyCompanyByMineCode(key));
            v.setWaterConsumptionCount(waterSettlementMapper.getWaterSettlementByMineCode(key));


            if (countVo != null) {
                v.setWaterChargesCount(countVo.getWaterChargesCount());
                v.setWaterResourcesChargesCount(countVo.getWaterResourcesChargesCount());
                v.setSewageTreatmentChargesCount(countVo.getSewageTreatmentChargesCount());
            }
            list.add(v);
        }
        return list;
    }

    @Override
    public List<ElectricityPlacePriceVo> getElectricityPlacePriceByMineCode(ElectricityPlacePriceReq param) {
        Map<String, String> map = new HashMap<>();
        List<ElectricityPlacePriceVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(param.getCompanyCode()) && StringUtils.isAllBlank(param.getMineCode(), param.getMineName())) {
            map = MineInfo.getNameByCompanyCodeOfMap(param.getCompanyCode());
        } else {
            map = MineInfo.getNameByMineCodeAndName(param.getCompanyCode(), param.getMineCode(), param.getMineName());
        }

        for (String key : map.keySet()) {
            ElectricityPlacePriceVo vCount = electricityPlacePriceMapper.getElectricityPlacePriceByMineCode(key);
            if(vCount==null|| StrUtil.isBlank(vCount.getElectricalLoadCount())){
                continue;
            }
            ElectricityPlacePriceVo v = new ElectricityPlacePriceVo();
            v.setCompanyCode(param.getCompanyCode());
            v.setMineCode(key);
            v.setMineName(map.get(key));
            if (null != vCount) {
                v.setElectricalLoadCount(vCount.getElectricalLoadCount());
            }
            list.add(v);
        }
        return list;
    }

    @Override
    public List<ElectricitySettlementVo> getElectricitySettlementVoByMineCode(ElectricitySettlementReq param) {

        Map<String, String> map = new HashMap<>();
        List<ElectricitySettlementVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(param.getCompanyCode()) && StringUtils.isAllBlank(param.getMineCode(), param.getMineName())) {
            map = MineInfo.getNameByCompanyCodeOfMap(param.getCompanyCode());
        } else {
            map = MineInfo.getNameByMineCodeAndName(param.getCompanyCode(), param.getMineCode(), param.getMineName());
        }

        for (String key : map.keySet()) {
            ElectricitySettlementVo vCount = electricitySettlementMapper.getElectricitySettlementByMineCode(key);
            if (vCount == null || (StrUtil.isBlank(vCount.getElectricalLoadCount()) && (vCount.getSettleElectricConsumptionCount() == null || vCount.getSettleElectricConsumptionCount().compareTo(new BigDecimal(0)) == 0) && (vCount.getSettleElectricAmountCount() == null || vCount.getSettleElectricAmountCount().compareTo(new BigDecimal(0)) == 0))) {
                continue;
            }
            ElectricitySettlementVo v = new ElectricitySettlementVo();
            v.setCompanyCode(param.getCompanyCode());
            v.setMineCode(key);
            v.setMineName(map.get(key));
            if (null != vCount) {
                v.setElectricalLoadCount(vCount.getElectricalLoadCount());
                v.setSettleElectricConsumptionCount(vCount.getSettleElectricConsumptionCount());
                v.setSettleElectricAmountCount(vCount.getSettleElectricAmountCount());
            }
            list.add(v);
        }
        return list;
    }

    //powerAnalysisService.selectPowerConsumptionAnalysisByYear(year,mineCode)
    @Override
    public List<PowerStatisticsInfoVo> getElectricitySettlementVoByMineCode(PowerStatisticsInfoReq param) {
        Calendar calendar = Calendar.getInstance();

        Map<String, String> map = new HashMap<>();
        List<PowerStatisticsInfoVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(param.getCompanyCode()) && StringUtils.isAllBlank(param.getMineCode(), param.getMineName())) {
            map = MineInfo.getNameByCompanyCodeOfMap(param.getCompanyCode());
        } else {
            map = MineInfo.getNameByMineCodeAndName(param.getCompanyCode(), param.getMineCode(), param.getMineName());
        }

        for (String key : map.keySet()) {
            PowerStatisticsInfoVo v = new PowerStatisticsInfoVo();
            v.setCompanyCode(param.getCompanyCode());
            v.setMineCode(key);
            v.setMineName(map.get(key));
            List<PowerStatisticsInfoVo> vList = powerAnalysisService.selectPowerConsumptionAnalysisByYear(calendar.get(Calendar.YEAR) + "", key);
            BigDecimal count = new BigDecimal(0);
            System.out.println("count:" + count);
            for (int i = 0; i < Calendar.MONTH + 1; i++) {
                count = count.add(vList.get(i).getHighest()).add(vList.get(i).getAverage()).add(vList.get(i).getTrough());
            }
            v.setYearElectricityCount(count);
            list.add(v);
        }
        return list;
    }

    @Override
    public HydropowerVo getWaterStatisticsByCompanyCodeOrMineCodeOrName(String companyCode, String mineCode) {
        Map<String, String> map = new HashMap<>();
        if(companyCode!=null&&!companyCode.equals("")&&(mineCode==null||mineCode=="")){
            map = MineInfo.getNameByCompanyCodeOfMap(companyCode);
        }else{
            map = MineInfo.getNameByMineCodeAndName(companyCode,mineCode,null);
        }

        StringBuffer value = new StringBuffer();
        for(String key : map.keySet()){
            value.append(key).append(",");
            //根据矿编码汇总数据
        }
        String keywordStr = value.deleteCharAt(value.length() - 1).toString();//去掉最后一个逗号
        map.clear();



        //按月份统计水
        Map<String,Object> waterConsumptionMap = statisticsMapper.getWaterConsumptionMonth(keywordStr);
        HydropowerVo vo = new HydropowerVo();
        if(waterConsumptionMap != null && !waterConsumptionMap.isEmpty()){
            String waterConsumption = waterConsumptionMap.get("waterconsumption") == null ? "0" : waterConsumptionMap.get("waterconsumption").toString();
            BigDecimal wa = new BigDecimal(waterConsumption).divide(new BigDecimal("10000"),2, BigDecimal.ROUND_CEILING);
            vo.setMonthWater(wa);
        }
        //按月份统计电
        Map<String,Object> settleElectricConsumptionMap = statisticsMapper.getSettleElectricConsumptionMonth(keywordStr);
        if(settleElectricConsumptionMap != null && !settleElectricConsumptionMap.isEmpty()){
            String settleelectricconsumption = settleElectricConsumptionMap.get("settleelectricconsumption") == null ? "0" : settleElectricConsumptionMap.get("settleelectricconsumption").toString();
            BigDecimal se = new BigDecimal(settleelectricconsumption).divide(new BigDecimal("10000"),2, BigDecimal.ROUND_CEILING);
            vo.setMonthElectricity(se);
        }
        //按年份统计水
        Map<String,Object> waterConsumptionYearMap = statisticsMapper.getWaterConsumptionYear(keywordStr);
        if(waterConsumptionYearMap != null && !waterConsumptionYearMap.isEmpty()){
            String waterConsumption = waterConsumptionYearMap.get("waterconsumption") == null ? "0" : waterConsumptionYearMap.get("waterconsumption").toString();
            BigDecimal wa = new BigDecimal(waterConsumption).divide(new BigDecimal("10000"),2, BigDecimal.ROUND_CEILING);
            vo.setYearWater(wa);
        }
        //按年份统计电
        Map<String,Object> settleElectricConsumptionYearMap = statisticsMapper.getSettleElectricConsumptionYear(keywordStr);
        if(settleElectricConsumptionYearMap != null && !settleElectricConsumptionYearMap.isEmpty()){
            String settleelectricconsumption = settleElectricConsumptionYearMap.get("settleelectricconsumption") == null ? "0" : settleElectricConsumptionYearMap.get("settleelectricconsumption").toString();
            BigDecimal se = new BigDecimal(settleelectricconsumption).divide(new BigDecimal("10000"),2, BigDecimal.ROUND_CEILING);
            vo.setYearElectricity(se);
        }
        return vo;
    }


}
